﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

//Reference: http://blogs.msdn.com/b/delay/archive/2009/08/31/get-out-of-the-way-with-the-tray-minimize-to-tray-sample-implementation-for-wpf.aspx
//add Context menu added.
namespace BootyBay.WPFAgent
{
    /// <summary>
    /// Class implementing support for "minimize to tray" functionality.
    /// </summary>
    public static class MinimizeToTray
    {
        /// <summary>
        /// Enables "minimize to tray" behavior for the specified Window.
        /// </summary>
        /// <param name="window">Window to enable the behavior for.</param>
        public static void Enable(Window window, ContextMenuStrip menu, Icon systemTrayIcon)
        {
            // No need to track this instance; its event handlers will keep it alive
            new MinimizeToTrayInstance(window, menu, systemTrayIcon);
        }

        /// <summary>
        /// Class implementing "minimize to tray" functionality for a Window instance.
        /// </summary>
        private class MinimizeToTrayInstance
        {
            private Window _window;
            private ContextMenuStrip _menu;
            private Icon _systemTrayIcon;

            private NotifyIcon _notifyIcon;
            private bool _balloonShown;

            /// <summary>
            /// Initializes a new instance of the MinimizeToTrayInstance class.
            /// </summary>
            /// <param name="window">Window instance to attach to.</param>
            public MinimizeToTrayInstance(Window window, ContextMenuStrip menu, Icon systemTrayIcon)
            {
                Debug.Assert(window != null, "window parameter is null.");
                _window = window;
                _window.StateChanged += new EventHandler(HandleStateChanged);
                _menu = menu;
                _systemTrayIcon = systemTrayIcon;
            }

            /// <summary>
            /// Handles the Window's StateChanged event.
            /// </summary>
            /// <param name="sender">Event source.</param>
            /// <param name="e">Event arguments.</param>
            private void HandleStateChanged(object sender, EventArgs e)
            {
                if (_notifyIcon == null)
                {
                    // Initialize NotifyIcon instance "on demand"
                    _notifyIcon = new NotifyIcon();

                    if (_systemTrayIcon != null)
                        _notifyIcon.Icon = _systemTrayIcon;
                    _notifyIcon.MouseClick += _notifyIcon_MouseClick;
                    //_notifyIcon.BalloonTipClicked += _notifyIcon_BalloonTipClicked;

                    _notifyIcon.ContextMenuStrip = _menu;
 
     
                }
                // Update copy of Window Title in case it has changed
                _notifyIcon.Text = _window.Title;

                // Show/hide Window and NotifyIcon
                var minimized = (_window.WindowState == WindowState.Minimized);
                _window.ShowInTaskbar = !minimized;
                _notifyIcon.Visible = true;

                if (minimized && !_balloonShown)
                {
                    // If this is the first time minimizing to the tray, show the user what happened
                    _notifyIcon.ShowBalloonTip(1000, null, _window.Title, ToolTipIcon.None);
                    _balloonShown = true;
                }
            }

            void _notifyIcon_BalloonTipClicked(object sender, EventArgs e)
            {
                _window.WindowState = WindowState.Normal;
            }

            void _notifyIcon_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    var minimized = (_window.WindowState == WindowState.Minimized);
                    _window.WindowState = minimized? WindowState.Normal: WindowState.Minimized;
                }

             
            }
 

 
 
        }
    }
}
